<template>
  <el-dialog
    :append-to-body="true"
    @close="closeDialog"
    :visible.sync="dialog"
    :title="is_edit?'编辑行业':'添加行业'"
    width="500px"
  >
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="行业名称" prop="name">
        <el-input placeholder="输入行业名称" v-model="form.name" style="width: 320px;" maxlength="20" />
      </el-form-item>
      <!-- <el-form-item label="所属行业" prop="pid">
        <treeselect v-model="form.pid" :options="menus" style="width: 320px;" placeholder="选择所属行业" />
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="loading" size="mini" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { _industryCreate, _industryUpdate, _industryAllGet } from "@/http/ajax";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect },
  name: "edit",
  props: {
    is_edit: {
      type: Boolean,
      required: true
    },
    cur_this: {
      type: Object,
      required: null
    }
  },
  data() {
    return {
      menus: [],
      dialog: false,
      loading: false,
      form: {
        name: "",
        pid: 0
      },
      rules: {
        name: [{ required: true, message: "请输入行业名称", trigger: "blur" }],
        pid: [{ required: true, message: "请选择所属行业", trigger: "blur" }]
      }
    };
  },
  created() {},
  methods: {
    doSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.dialog = true;
          if (this.is_edit) {
            _industryUpdate(
              this.form,
              res => {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
                this.dialog = false;
                this.cur_this.getList();
                this.dataInit();
              },
              err => {}
            );
          } else {
            _industryCreate(
              this.form,
              res => {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
                this.dialog = false;
                this.cur_this.getList();
                this.dataInit();
              },
              err => {}
            );
          }
        }
      });
    },
    domInit() {
      this.getMenus();
    },
    dataInit() {
      this.form = {
        name: "",
        pid: 0
      }
    },
    getMenus(file, fileList) {
      _industryAllGet(
        res => {
          this.menus = [];
          const menu = { id: 0, label: "顶级类目", children: [] };
          let arr = [];
          res.forEach(e => {
            arr.push({ id: e.id, label: e.name });
          });
          menu.children = arr;
          this.menus.push(menu);
        },
        err => {}
      );
    },
    closeDialog() { this.$refs["form"].resetFields();}
  }
};
</script>

<style lang="scss">
</style>
